package com.roc.web.action.merinfo;
import java.util.ArrayList;
import java.util.List;

import com.roc.page.BaseSearchCondition;
import com.roc.page.WhereHQLFieldBean;
import com.roc.util.Constance;
import com.roc.util.constant.Role;


public class MerinfoChangeSearchCondition extends BaseSearchCondition {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String  orgCode;//收单机构号
	private String obj;//所有下级代理
	private String opId;//操作员id
	private String roleId; //角色 
	
	private String merchantNo;//商户号
	private String merchantName;//商户名称
	private String merName;//营业名称
	
	private String areaCode;//地区
	private String cardNo; //身份证
	private String addr;//商户地址
	private String oneOrgCode;//机构号
	private String agentId;//代理商id
	private String groupCd; //受理市场标识
	
	private String YYagentId;//运营所属机构
	
	
	
	@Override
	protected List<WhereHQLFieldBean> getWhereHQLFieldBeanList() {
		List<WhereHQLFieldBean> filedBeanList = new ArrayList<WhereHQLFieldBean>();
        if(Constance.isNotEmpty(orgCode)){ //所属收单机构
                filedBeanList.add(new WhereHQLFieldBean("A.BANK_BELONG_CD", orgCode.trim(), "=", false));
        }
      //机构
      	if(Constance.isNotEmpty(oneOrgCode)&&!roleId.equals(Role.SYSTEM.getCode())&&!roleId.equals(Role.AGENT.getCode())&&!(roleId.equals(Role.MARKET_DIRECT.getCode())||roleId.equals(Role.MARKET_LEAD.getCode()))){//机构
      			filedBeanList.add(new WhereHQLFieldBean("A.uc_bc_cd_area", oneOrgCode.trim(), "=", false));
        } 
        if(Constance.isNotEmpty(opId) && Constance.isNotEmpty(roleId) && roleId.equals(Role.AGENT_BUSS.getCode())){//业务员
            filedBeanList.add(new WhereHQLFieldBean("A.REC_UPD_OPR", opId.trim(), "=", false));
        }
		if(Constance.isNotEmpty(merchantNo)){ //商户号
			filedBeanList.add(new WhereHQLFieldBean("A .MCHT_CD", merchantNo.trim(), "=", false));
		}
		if(Constance.isNotEmpty(merchantName)){ //商户名称 （模糊）
			filedBeanList.add(new WhereHQLFieldBean("A .NAME_BUSI", merchantName.trim(), "like", false,"ALL"));
		}
		if(Constance.isNotEmpty(merName)){ //营业名称 （模糊）
                    filedBeanList.add(new WhereHQLFieldBean("A . NAME", merName.trim(), "like", false,"ALL"));
		}
		if(Constance.isNotEmpty(areaCode)){ //地区
			if(!areaCode.equals("99")){
				filedBeanList.add(new WhereHQLFieldBean("A .prov_cd", areaCode.trim(), "=", false));
			}
		}
		if(Constance.isNotEmpty(cardNo)){ //法人身份证号
			filedBeanList.add(new WhereHQLFieldBean("A .CERTIF_NO", cardNo.trim(), "=", false));
		}
		if(Constance.isNotEmpty(groupCd)){ //受理市场标识
			filedBeanList.add(new WhereHQLFieldBean("A.group_cd", groupCd.trim(), "=", false));
		}
		if(Constance.isNotEmpty(addr)){ //商户地址 （模糊）
			filedBeanList.add(new WhereHQLFieldBean("A .REG_ADDR", addr.trim(), "like", false,"ALL"));
		}
		return filedBeanList;
	}

	@Override
	protected String getRowCountHQL() {
    	        StringBuffer  sb= new StringBuffer();
    	        sb.append(" select count(1)                                                               \n");
    	        sb.append("   from TBL_MCHT_INF A, TBL_INS_INF C, TBL_MCHT_BANKACCOUNT D, t_operators T   \n");
    	        sb.append("  where A.bank_belong_cd = C.INS_ID_CD                                         \n");
    	        sb.append("    and A.MCHT_CD = D.OWNER_CD                                                 \n");
    	        sb.append("    and A.ext1 = to_char(T.ID)                                                 \n");
    	        sb.append("    and a.status = '01'                                                        \n");
    	        sb.append("    and NOT exists (SELECT mid                                                 \n");
    	        sb.append("           FROM T_MERINFO_CHANGE MC                                            \n");
    	        sb.append("          WHERE (MC.status = 'W' OR MC.status = 'L')                           \n");
    	        sb.append("            and A.MCHT_CD = MC.mid)                                            \n");
    	        //登陆人员为运维人员
    	        if(roleId.equals(Role.OPERATORS_COMM.getCode())){
    	        	 sb.append("  and A.EXT1= ").append(YYagentId).append("   \n");
    	        }
    	        //登陆人员为机构管理员
    	        if(roleId.equals(Role.O_MANAGER.getCode())){
    	        	sb.append("  and A.EXT1= ").append(opId).append("   \n");
    	        }
    	       //角色为代理商或商服宝会员或父级id为代理商            
    		   if(roleId.equals(Role.AGENT.getCode())||roleId.equals(Role.SFB_AGENT.getCode())){
    			   sb.append("   and  A.EXT1 = ").append(opId).append("   \n");
    			   
    			   
	    	    /*    sb.append("    and exists(select 1                                                        \n");
	    	        sb.append("           from tbl_mer_agent_relation ma                                      \n");
	    	        sb.append("          where ma.merno = a.mcht_cd                                           \n");
	    	        sb.append("            and ma.agentid =").append(opId).append(" ) \n");*/
    			}
    		   //登录人角色销售人员                   
    			if(roleId.equals(Role.MARKET_DIRECT.getCode())||roleId.equals(Role.MARKET_LEAD.getCode())){                                               
	    	        sb.append("    and exists (select 1                                                      \n");
	    	        sb.append("                   from t_oper_role orole           \n");
	    	        sb.append("                  where a.uc_bc_cd_area = orole.agentid                             \n");
	    	        sb.append("                    and orole.operid =").append(opId).append("   \n");
    			}
    			//登陆人员为业务员
    			if(roleId.equals(Role.AGENT_BUSS.getCode())){
    				sb.append("  and A.REC_UPD_OPR= ").append(opId).append("   \n");
    			}
    			
                return sb.toString();
	}

	@Override
	protected String getSearchHQL() {
	        StringBuffer  sb= new StringBuffer();
	        
    	    sb.append(" SELECT A.MCHT_CD,A.NAME_BUSI,A.NAME MERNAME,C.INS_NAME,A.CONTACT_NAME,");
            sb.append(" A.CONTACT_PHONENO,A.CERTIF,A.CERTIF_NO,A.BUSI_LICE_NO, A.REG_ADDR,A.STATUS,A.prov_cd,");
            sb.append(" T.REAL_NAME,D.NAME accountname,D.ACCOUNT,D.BANK_NAME,D.BANK_CODE,");
            sb.append(" CASE '1000' WHEN (SELECT DISTINCT PROD_CD FROM TBL_MCHT_BIZ_DEAL y WHERE y.MCHT_CD = A .MCHT_CD AND y.PROD_CD = '1000') THEN 'T1' ELSE '0' END  T1_type, ");
            sb.append(" CASE '1171' WHEN (SELECT DISTINCT PROD_CD FROM TBL_MCHT_BIZ_DEAL y WHERE y.MCHT_CD = A .MCHT_CD AND y.PROD_CD = '1171') THEN 'T0' ELSE '0' END  T0_type,a.ext1 ");
            sb.append("   from TBL_MCHT_INF A, TBL_INS_INF C, TBL_MCHT_BANKACCOUNT D, t_operators T   \n");
	        sb.append("  where A.bank_belong_cd = C.INS_ID_CD                                         \n");
	        sb.append("    and A.MCHT_CD = D.OWNER_CD                                                 \n");
	        sb.append("    and A.ext1 = to_char(T.ID)                                                 \n");
	        sb.append("    and a.status = '01'                                                        \n");
	        sb.append("    and NOT exists (SELECT mid                                                 \n");
	        sb.append("           FROM T_MERINFO_CHANGE MC                                            \n");
	        sb.append("          WHERE (MC.status = 'W' OR MC.status = 'L')                             \n");
	        sb.append("            and A.MCHT_CD = MC.mid    )                                         \n");        
	        //登陆人员为运维人员
	        if(roleId.equals(Role.OPERATORS_COMM.getCode())){
	        	 sb.append("  and A.EXT1= ").append(YYagentId).append("   \n");
	        }
	        //登陆人员为机构管理员
	        if(roleId.equals(Role.O_MANAGER.getCode())){
	        	sb.append("  and A.EXT1= ").append(opId).append("   \n");
	        }
	       //角色为代理商或商服宝会员或父级id为代理商            
		   if(roleId.equals(Role.AGENT.getCode())||roleId.equals(Role.SFB_AGENT.getCode())){
			   sb.append("   and  A.EXT1 = ").append(opId).append("   \n");
			   
			   
    	    /*    sb.append("    and exists(select 1                                                        \n");
    	        sb.append("           from tbl_mer_agent_relation ma                                      \n");
    	        sb.append("          where ma.merno = a.mcht_cd                                           \n");
    	        sb.append("            and ma.agentid =").append(opId).append(" ) \n");*/
			}
		   //登录人角色销售人员                   
			if(roleId.equals(Role.MARKET_DIRECT.getCode())||roleId.equals(Role.MARKET_LEAD.getCode())){                                               
    	        sb.append("    and exists (select 1                                                      \n");
    	        sb.append("                   from t_oper_role orole           \n");
    	        sb.append("                  where a.uc_bc_cd_area = orole.agentid                             \n");
    	        sb.append("                    and orole.operid =").append(opId).append("    \n");
			}
			//登陆人员为业务员
			if(roleId.equals(Role.AGENT_BUSS.getCode())){
				sb.append("  and A.REC_UPD_OPR= ").append(opId).append("    \n");
			}
	        return sb.toString();
	}

	@Override
        protected String getOrderPartOfHQL() {
                return " ORDER BY A.REC_CRT_TS DESC  ";
        }
	public String getOrgCode() {
		return orgCode;
	}

	public void setOrgCode(String orgCode) {
		this.orgCode = orgCode;
	}

	public String getMerchantNo() {
		return merchantNo;
	}

	public void setMerchantNo(String merchantNo) {
		this.merchantNo = merchantNo;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}
        public String getObj() {
            return obj;
        }
    
        public void setObj(String obj) {
            this.obj = obj;
        }

        public String getOpId() {
            return opId;
        }

        public void setOpId(String opId) {
            this.opId = opId;
        }
    	

        public String getAddr() {
                return addr;
        }

        public void setAddr(String addr) {
                this.addr = addr;
        }

        public String getMerName() {
            return merName;
        }

        public void setMerName(String merName) {
            this.merName = merName;
        }

        public String getRoleId() {
            return roleId;
        }

        public void setRoleId(String roleId) {
            this.roleId = roleId;
        }

        public String getAgentId() {
            return agentId;
        }

        public void setAgentId(String agentId) {
            this.agentId = agentId;
        }

		public String getOneOrgCode() {
			return oneOrgCode;
		}

		public void setOneOrgCode(String oneOrgCode) {
			this.oneOrgCode = oneOrgCode;
		}

		public String getGroupCd() {
			return groupCd;
		}

		public void setGroupCd(String groupCd) {
			this.groupCd = groupCd;
		}

		public String getYYagentId() {
			return YYagentId;
		}

		public void setYYagentId(String yYagentId) {
			YYagentId = yYagentId;
		}
        
        
}
